package com.xinghai.system.service.impl;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.xinghai.common.utils.DateUtils;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.system.mapper.QdChangeOrderMapper;
import com.xinghai.system.domain.QdChangeOrder;
import com.xinghai.system.service.IQdChangeOrderService;

/**
 * 订单变更Service业务层处理
 *
 * @author xinghai
 * @date 2024-12-08
 */
@Service
public class QdChangeOrderServiceImpl extends ServiceImpl<QdChangeOrderMapper, QdChangeOrder>
        implements IQdChangeOrderService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询订单变更列表
     *
     * @param qdChangeOrder 订单变更
     * @return 订单变更集合
     */
    @Override
    public List<QdChangeOrder> list(QdChangeOrder qdChangeOrder) {
        QueryWrapper<QdChangeOrder> queryWrapper = new QueryWrapper<>();
        if (qdChangeOrder.getStatus()!= null  && !qdChangeOrder.getStatus().isEmpty()){
            queryWrapper.eq("status", qdChangeOrder.getStatus());
        }
        if (qdChangeOrder.getParams().get("beginCreateTime")!= null && !"".equals(qdChangeOrder.getParams().get("beginCreateTime"))
        &&qdChangeOrder.getParams().get("endCreateTime")!= null && !"".equals(qdChangeOrder.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", qdChangeOrder.getParams().get("beginCreateTime"),qdChangeOrder.getParams().get("endCreateTime"));
        }
        if (qdChangeOrder.getOrderId()!= null ){
            queryWrapper.eq("order_id", qdChangeOrder.getOrderId());
        }
        if (qdChangeOrder.getOldUserId()!= null ){
            queryWrapper.eq("old_user_id", qdChangeOrder.getOldUserId());
        }
        if (qdChangeOrder.getOldUserName()!= null  && !qdChangeOrder.getOldUserName().isEmpty()){
            queryWrapper.like("old_user_name", qdChangeOrder.getOldUserName());
        }
        if (qdChangeOrder.getOldUserPhone()!= null  && !qdChangeOrder.getOldUserPhone().isEmpty()){
            queryWrapper.eq("old_user_phone", qdChangeOrder.getOldUserPhone());
        }
        if (qdChangeOrder.getOldStoreId()!= null ){
            queryWrapper.eq("old_store_id", qdChangeOrder.getOldStoreId());
        }
        if (qdChangeOrder.getOldStoreName()!= null  && !qdChangeOrder.getOldStoreName().isEmpty()){
            queryWrapper.like("old_store_name", qdChangeOrder.getOldStoreName());
        }
        if (qdChangeOrder.getOldStoreLeader()!= null  && !qdChangeOrder.getOldStoreLeader().isEmpty()){
            queryWrapper.eq("old_store_leader", qdChangeOrder.getOldStoreLeader());
        }
        if (qdChangeOrder.getOldStoreLeaderId()!= null ){
            queryWrapper.eq("old_store_leader_id", qdChangeOrder.getOldStoreLeaderId());
        }
        if (qdChangeOrder.getOldStoreLeaderPhone()!= null  && !qdChangeOrder.getOldStoreLeaderPhone().isEmpty()){
            queryWrapper.eq("old_store_leader_phone", qdChangeOrder.getOldStoreLeaderPhone());
        }
        if (qdChangeOrder.getType()!= null  && !qdChangeOrder.getType().isEmpty()){
            queryWrapper.eq("type", qdChangeOrder.getType());
        }
        if (qdChangeOrder.getNewUserId()!= null ){
            queryWrapper.eq("new_user_id", qdChangeOrder.getNewUserId());
        }
        if (qdChangeOrder.getNewUserName()!= null  && !qdChangeOrder.getNewUserName().isEmpty()){
            queryWrapper.like("new_user_name", qdChangeOrder.getNewUserName());
        }
        if (qdChangeOrder.getNewUserPhone()!= null  && !qdChangeOrder.getNewUserPhone().isEmpty()){
            queryWrapper.eq("new_user_phone", qdChangeOrder.getNewUserPhone());
        }
        if (qdChangeOrder.getReceiveStatus()!= null  && !qdChangeOrder.getReceiveStatus().isEmpty()){
            queryWrapper.eq("receive_status", qdChangeOrder.getReceiveStatus());
        }
        if (qdChangeOrder.getNewStoreId()!= null ){
            queryWrapper.eq("new_store_id", qdChangeOrder.getNewStoreId());
        }
        if (qdChangeOrder.getNewStoreName()!= null  && !qdChangeOrder.getNewStoreName().isEmpty()){
            queryWrapper.like("new_store_name", qdChangeOrder.getNewStoreName());
        }
        if (qdChangeOrder.getNewStoreLeader()!= null  && !qdChangeOrder.getNewStoreLeader().isEmpty()){
            queryWrapper.eq("new_store_leader", qdChangeOrder.getNewStoreLeader());
        }
        if (qdChangeOrder.getNewStoreLeaderId()!= null ){
            queryWrapper.eq("new_store_leader_id", qdChangeOrder.getNewStoreLeaderId());
        }
        if (qdChangeOrder.getNewStoreLeaderPhone()!= null  && !qdChangeOrder.getNewStoreLeaderPhone().isEmpty()){
            queryWrapper.eq("new_store_leader_phone", qdChangeOrder.getNewStoreLeaderPhone());
        }
        return this.list(queryWrapper);
    }

    /**
     * 删除订单变更信息
     * @param id 需要删除的数据ID
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    public boolean delete(Long id, boolean flag) {
        if (flag){
            QdChangeOrder qdChangeOrder = new QdChangeOrder();
            qdChangeOrder.setId(id);
            qdChangeOrder.setDelFlag(GenConstants.DELETE_FLAG_YES);
            return this.updateById(qdChangeOrder);
        }
        return this.removeById(id);
    }

    /**
     * 批量删除订单变更信息
     *
     * @param ids 需要删除的数据ID集合
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids, boolean flag) {
        if (flag){
            List<QdChangeOrder> collect = Arrays.stream(ids).map(m -> {
                QdChangeOrder qdChangeOrder = new QdChangeOrder();
                qdChangeOrder.setId(m);
                qdChangeOrder.setDelFlag(GenConstants.DELETE_FLAG_YES);
                return qdChangeOrder;
            }).collect(Collectors.toList());
            return this.updateBatchById(collect);
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 导入订单变更信息
     *
     * @param list 订单变更信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<QdChangeOrder> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            QdChangeOrderMapper qdChangeOrderMapper = sqlSession.getMapper(QdChangeOrderMapper.class);
            list.forEach(qdChangeOrderMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
